package fr.upstlse.glre.client.administration.utilisateurs;

import com.extjs.gxt.ui.client.Events;
import com.extjs.gxt.ui.client.event.ComponentEvent;
import com.extjs.gxt.ui.client.event.SelectionListener;
import com.extjs.gxt.ui.client.widget.Info;
import com.extjs.gxt.ui.client.widget.MessageBox;
import com.extjs.gxt.ui.client.widget.Window;
import com.extjs.gxt.ui.client.widget.button.Button;
import com.extjs.gxt.ui.client.widget.button.ToolButton;
import com.extjs.gxt.ui.client.widget.form.FormPanel;
import com.google.gwt.user.client.rpc.AsyncCallback;

import fr.upstlse.glre.client.RfgConstante;
import fr.upstlse.glre.client.administration.utilisateurs.action.DeleteUserAction;
import fr.upstlse.glre.client.administration.utilisateurs.action.ModifyUserAction;
import fr.upstlse.glre.client.administration.utilisateurs.action.RoleSelectionAction;
import fr.upstlse.glre.client.espacededie.component.IUserModification;
import fr.upstlse.glre.client.utilitaires.ControlerUtil;
import fr.upstlse.glre.client.vo.user.EtudiantVOGWT;
import fr.upstlse.glre.client.vo.user.ProfesseurVOGWT;
import fr.upstlse.glre.client.vo.user.UserRoleVOGWT;
import fr.upstlse.glre.client.vo.user.UserVOGWT;
import fr.upstlse.glre.client.widgets.fields.EtudiantFields;
import fr.upstlse.glre.client.widgets.fields.IUserFields;
import fr.upstlse.glre.client.widgets.fields.ProfesseurFields;
import fr.upstlse.glre.client.widgets.fields.UserFields;

public class ModificationWindow extends Window implements IUserModification {

	private UserVOGWT user;
	private boolean editionMode;

	private FormPanel formPanel;
	private IUserFields fields;
	private IUserFields additionnalFields;

	private static ModificationWindow instance;

	protected ModificationWindow(UserVOGWT pUserVOGWT) {
		super();
		user = pUserVOGWT;
		setId(RfgConstante.ADMIN_UTILISATEURS_WINDOW_ID);
		setHeading(RfgConstante.ADMIN_UTILISATEURS_WINDOW_HEADER_TEXT);

		initWidget();
	}

	private void initWidget() {
		editionMode = false;
		readMode();
		// Tool Button
		ToolButton lSwithToEditionMode = new ToolButton(".my-tool-gear");
		lSwithToEditionMode.setStyleAttribute("background",
				" transparent url(images/edit.png) no-repeat");
		lSwithToEditionMode
				.setToolTip(RfgConstante.PROFIL_PANEL_BUTTON_SWITCH_TOOLTIP);
		lSwithToEditionMode
				.addSelectionListener(new SelectionListener<ComponentEvent>() {
					public void componentSelected(ComponentEvent ce) {
						if (!editionMode) {
							removeAll();
							editionMode();
							editionMode = true;
							layout();
						} else {
							removeAll();
							readMode();
							editionMode = false;
							layout();
						}
					}
				});
		getHeader().addTool(lSwithToEditionMode);
		createDeleteButton();
	}

	private void createDeleteButton() {
		ToolButton lDeleteButton = new ToolButton(".my-tool-gear");
		lDeleteButton.setStyleAttribute("background",
				" transparent url(images/supprimer.png) no-repeat");
		lDeleteButton.setToolTip(RfgConstante.DELETE_BUTTON_TOOLTIP
				+ " l'utilisateur");
		lDeleteButton
				.addSelectionListener(new SelectionListener<ComponentEvent>() {
					public void componentSelected(ComponentEvent ce) {
						MessageBox.confirm("Confirmation",
								"Voulez-vous supprimer l'utilisateur "
										+ user.getLogin() + " ?",
								new DeleteUserAction(user));
					}
				});
		getHeader().addTool(lDeleteButton);
	}

	private void readMode() {
		add(user.getHtml());
	}

	private void editionMode() {
		// formulaire sans action
		formPanel = new FormPanel();
		formPanel.setHeaderVisible(false);
		formPanel.setAutoWidth(true);

		if ((user.getRole().equals(UserRoleVOGWT.ADMIN.getValue())) || (user.getRole().equals(UserRoleVOGWT.SECRETAIRE.getValue()))) {
			fields = new UserFields(user, formPanel);
			fields.getRole().addListener(Events.Select,
					new RoleSelectionAction(this));
			Button lSubmit = new Button(
					RfgConstante.PROFIL_EDITING_FIELD_SUBMIT_LABEL);
			lSubmit.addSelectionListener(new ModifyUserAction(fields));
			formPanel.addButton(lSubmit);

			add(formPanel);
		} else if (user.getRole().equals(UserRoleVOGWT.ETUDIANT.getValue())) {
			final MessageBox lWaitMessageBox = MessageBox.wait("Information",
					"Chargement des informations de l'étudiant ",
					"chargement en cours...");
			AsyncCallback<EtudiantVOGWT> lCallback = new AsyncCallback<EtudiantVOGWT>() {
				public void onFailure(Throwable pCaught) {
					lWaitMessageBox.hide();
					MessageBox.alert("Erreur getEtudiant ",
							pCaught.getMessage(), null);
				}

				public void onSuccess(EtudiantVOGWT pResult) {
					lWaitMessageBox.hide();
					EtudiantVOGWT lEtudiantVOGWT = pResult;
					Info.display("Information", "Étudiant chargé avec succès",
							"");
					fields = new EtudiantFields(lEtudiantVOGWT, formPanel, true);
					Button lSubmit = new Button(
							RfgConstante.PROFIL_EDITING_FIELD_SUBMIT_LABEL);
					lSubmit.addSelectionListener(new ModifyUserAction(fields));
					formPanel.addButton(lSubmit);

					add(formPanel);
					layout();
				}
			};
			ControlerUtil.getUserControlerServiceAsyncInstance()
					.getEtudiantById(user.getId(), lCallback);
		} else if (user.getRole().equals(UserRoleVOGWT.PROFESSEUR.getValue())) {
			final MessageBox lWaitMessageBox = MessageBox.wait("Information",
					"Chargement des informations du professeur ",
					"chargement en cours...");
			AsyncCallback<ProfesseurVOGWT> lCallback = new AsyncCallback<ProfesseurVOGWT>() {
				public void onFailure(Throwable pCaught) {
					lWaitMessageBox.hide();
					MessageBox.alert("Erreur getProfesseur ", pCaught
							.getMessage(), null);
				}

				public void onSuccess(ProfesseurVOGWT pResult) {
					lWaitMessageBox.hide();
					ProfesseurVOGWT lProfesseurVOGWT = (ProfesseurVOGWT) pResult;
					Info.display("Information",
							"Professeur chargé avec succès", "");
					fields = new ProfesseurFields(lProfesseurVOGWT, formPanel,
							true);
					Button lSubmit = new Button(
							RfgConstante.PROFIL_EDITING_FIELD_SUBMIT_LABEL);
					lSubmit.addSelectionListener(new ModifyUserAction(fields));
					formPanel.addButton(lSubmit);

					add(formPanel);
					layout();
				}
			};
			ControlerUtil.getUserControlerServiceAsyncInstance()
					.getProfesseurById(user.getId(), lCallback);
		} else {
			MessageBox.alert("Erreur", "Role non pris en charge ("
					+ user.getRole() + ")", null);
		}

	}

	public static ModificationWindow getInstance(UserVOGWT pUserVOGWT) {
		if (instance == null) {
			instance = new ModificationWindow(pUserVOGWT);
		}

		return instance;
	}

	public static void closeCurrentWindow() {
		if (instance != null) {
			instance.hide();
			instance = null;
		}
	}

	public IUserFields getAdditionalFields() {
		return additionnalFields;
	}

	public FormPanel getFormPanel() {
		return formPanel;
	}

	public void setAdditionalFields(IUserFields pAdditionalFields) {
		additionnalFields = pAdditionalFields;

	}

	public void setFormPanel(FormPanel pFormPanel) {
		formPanel = pFormPanel;

	}

}
